package com.lin.views;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


import com.lin.utils.Parameters;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.PinterestCircleBitmapDisplayer;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.bean.RecentCallsBean;
import com.yunfeng.supercall.bean.RecentCallsBean;
import com.yunfeng.supercall.bean.UserBean;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewForPersonalCardHistoryCall extends ListView{
	SimpleDateFormat mSimpleDateFormat;
	SimpleDateFormat mSimpleDateFormat1;
	MyAdapter mFocusGameAdapter;
	Context mContext;
	DisplayImageOptions options;

	OnItemListViewEventListener mOnItemListViewEventListener;
	private UserBean mUser;
	public ListViewForPersonalCardHistoryCall(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		mSimpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		mContext = context;
		options = new DisplayImageOptions.Builder()
		.displayer(new PinterestCircleBitmapDisplayer(context.getResources().getDimensionPixelOffset(R.dimen.corners_avatar)))
		.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
		mUser = BaseActivity.getUser(context);
	}

	public void setOnItemListViewEventListener(OnItemListViewEventListener mOnItemListViewEventListener) {
		this.mOnItemListViewEventListener = mOnItemListViewEventListener;
	}

	public interface OnItemListViewEventListener {
		public void onItemAvatarLisener(RecentCallsBean maListInfo, int x, int y);
		public void onItemClickListener(RecentCallsBean maListInfo, int x, int y);
		public void onItemUserDetailListener(RecentCallsBean maListInfo, int x, int y);
	}


	public void setDataList(ArrayList<RecentCallsBean> mFocusGameList) {
		if (mFocusGameAdapter == null) {
			mFocusGameAdapter = new MyAdapter(getContext(), mFocusGameList);
			setAdapter(mFocusGameAdapter);
		} else {
			mFocusGameAdapter.setmFocusGameList(mFocusGameList);
			mFocusGameAdapter.notifyDataSetChanged();
		}
	}

	private class MyAdapter extends BaseAdapter implements OnClickListener{

		LayoutInflater mLayoutInflater;
		ArrayList<RecentCallsBean> mDataList;
		int mSize;




		public MyAdapter(Context context,
				ArrayList<RecentCallsBean> mDataList) {
			// TODO Auto-generated constructor stub
			this.mDataList = mDataList;
			mLayoutInflater =  LayoutInflater.from(mContext);
		}

		public ArrayList<RecentCallsBean> getDataList() {
			return mDataList;
		}

		public void setmFocusGameList(ArrayList<RecentCallsBean> mFocusGameList) {
			this.mDataList = mFocusGameList;
		}


		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			mSize = mDataList.size();
			return mSize;
		}


		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}







		@Override
		public RecentCallsBean getItem(int position) {
			// TODO Auto-generated method stub
			return mDataList.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder viewholder = null;

			int type = getItemViewType(position);
			if (convertView == null) {
				viewholder = new ViewHolder();
				convertView = mLayoutInflater.inflate(
						R.layout.list_item_call_history, null);
				viewholder.tvTime = (TextView) convertView.findViewById(R.id.textview_time);
				viewholder.tvState = (TextView) convertView.findViewById(R.id.textview_state);
				viewholder.ivIcon = (ImageView) convertView.findViewById(R.id.imageview);
				convertView.setTag(viewholder);

			} else {
				viewholder = (ViewHolder) convertView.getTag();
			}

			
			RecentCallsBean base = getItem(position);
			
			int callType = 0;
			if (base.getUser1() == mUser.getUserid()) {
				callType = RecentCallsBean.TYPE_OUT_CALL;
			} else {
				callType = RecentCallsBean.TYPE_IN_CALL;
			}
			if (base.getState() == 0) {
				if (base.getUser1() == mUser.getUserid()) {
					callType = RecentCallsBean.TYPE_MISSED_CALL;
				} else {
					callType = RecentCallsBean.TYPE_MISSED_CALL_OTHER;
				}

			}
			
			switch (callType) {
			case RecentCallsBean.TYPE_IN_CALL:
				viewholder.ivIcon.setImageResource(R.drawable.ic_call_in);
				viewholder.tvState.setTextColor(Color.rgb(23, 202, 44));
				viewholder.tvState.setText("打入");
				break;
			case RecentCallsBean.TYPE_OUT_CALL:
				viewholder.ivIcon.setImageResource(R.drawable.ic_call_out);
				viewholder.tvState.setTextColor(Color.rgb(23, 202, 44));
				viewholder.tvState.setText("打出");
				break;
			case RecentCallsBean.TYPE_MISSED_CALL:
				viewholder.ivIcon.setImageResource(R.drawable.ic_missed_call_other);
				viewholder.tvState.setTextColor(Color.rgb(236, 46, 14));
				viewholder.tvState.setText("未接电话");
				break;
			case RecentCallsBean.TYPE_MISSED_CALL_OTHER:
				viewholder.ivIcon.setImageResource(R.drawable.ic_call_missed);
				viewholder.tvState.setTextColor(Color.rgb(236, 46, 14));
				viewholder.tvState.setText("未接通");
				break;
			}


			long date = 0;
			try {
				date = mSimpleDateFormat1.parse(base.getCalltime()).getTime();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// long date = new cale
			viewholder.tvTime.setText(generateTime(date));
			return convertView;

		}



		void setLevelColor(int level,TextView tv, TextView tvLevel) {
			try {

				if(level == 0) {
					tvLevel.setTextColor(getResources().getColor(R.color.textview_title_listview));
					tv.setTextColor(getResources().getColor(R.color.textview_title_listview));	
				} else if(level == 1) {
					tvLevel.setTextColor(getResources().getColor(R.color.level_or));
					tv.setTextColor(getResources().getColor(R.color.level_or));
				} else {
					tvLevel.setTextColor(getResources().getColor(R.color.level_red));
					tv.setTextColor(getResources().getColor(R.color.level_red));
				}
			} catch(NullPointerException e) {

			}
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int[] mLocation = new int[2];
			v.getLocationInWindow(mLocation);
			RecentCallsBean mDownloadListInfo = (RecentCallsBean) v.getTag();
			if(mOnItemListViewEventListener != null) { // if we don't define this , there'll be the null pointer exception when we click an item if not setOnXXListener not work
				if(v.getId() == R.id.iv) {
					mOnItemListViewEventListener.onItemAvatarLisener(mDownloadListInfo, mLocation[0], mLocation[1]);
				} else if(v.getId() == R.id.linearlayout) {
					mOnItemListViewEventListener.onItemClickListener(mDownloadListInfo, mLocation[0], mLocation[1]);
				} else if(v.getId() == R.id.is_super) {
					mOnItemListViewEventListener.onItemUserDetailListener(mDownloadListInfo, mLocation[0], mLocation[1]);
				}
			}

		}
	}

	public String generateTime(long date) {

		Date date2 = new Date(date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date2);
		Calendar now = Calendar.getInstance();
		int count = now.get(Calendar.DAY_OF_YEAR)
				- calendar.get(Calendar.DAY_OF_YEAR);
		if (count == 1) {
			return "昨天 " + calendar.get(Calendar.HOUR_OF_DAY) + ":"
					+ calendar.get(Calendar.MINUTE);
		} else if (count == 0) {
			int minu = calendar.get(Calendar.MINUTE);
			return calendar.get(Calendar.HOUR_OF_DAY) + ":"
			+ (minu < 10 ? "0" : "") + minu;
		}
		return mSimpleDateFormat.format(date2);

	}


	static class ViewHolder {
		TextView tvState;
		ImageView ivIcon;
		TextView tvTime;
	}
}